package com.hsnn.medstgmini.settlement.controller;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.dict.model.form.DicAreaForm;
import com.hsnn.medstgmini.base.dict.service.DicAreaManager;
import com.hsnn.medstgmini.base.std.model.HospInfo;
import com.hsnn.medstgmini.exception.MSException;
import com.hsnn.medstgmini.model.Whether;
import com.hsnn.medstgmini.settlement.model.SetSettlement;
import com.hsnn.medstgmini.settlement.model.SetSettlementDetail;
import com.hsnn.medstgmini.settlement.model.form.SetSettlementForm;
import com.hsnn.medstgmini.settlement.model.form.SetSettlementfirstForm;
import com.hsnn.medstgmini.settlement.service.SettlementFirstManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.sys.service.SysUserManager;
import com.hsnn.medstgmini.util.AreaUtil;
import com.hsnn.medstgmini.util.DateUtil;
import com.hsnn.medstgmini.util.DbUtil;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.ParseNumber;

/**
 * 
 * @category 一级结算单
 * @author 邱磊
 * @date 2015年7月22日 上午9:44:05
 */
@Controller
@RequestMapping("/settlementFirst")
public class SettlementFirstController {
	private static final Logger logger = Logger
			.getLogger(SettlementFirstController.class);
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private SettlementFirstManager manager;

	@Autowired
	private SysUserManager sysUserManager;

	@Autowired
	private DicAreaManager dicAreaManager;
	@Value("#{sysProp['system.province']}")
	private String sysProvince;

	/**
	 * 
	 * @category 保存一级结算单信息（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月22日 下午3:58:37
	 * @param data
	 * @return
	 */
	@RequestMapping("/saveSettlement")
	@ResponseBody
	public Pagination saveSettlement(SetSettlement data) {
		Pagination page = new Pagination();
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String hospId = ((HospInfo) user.getInfo()).getHospId();
		data.setOrgId(hospId);
		// data.setFirstlevelSettlementName(((HospInfo) user.getInfo())
		// .getHospName() + ParseDate.parseShortFormat(new Date()) + "结算单");
		data.setSettlementName(data.getSettlementName());
		data.setCreateUser(user.getId());
		data.setCreateDatetime(new Date());
		data.setSettlementType(1);
		data.setTotalOrders(0);
		data.setPayStatus(0);
		data.setTotalAmount(0.00);
		data.setStatus(0);
		try {
			if (manager.checkSameName(data)) {
				page.setSuccess(false);
				page.setMsg("结算单名称重复，请重新输入！");
			} else {
				manager.saveSettlementFirst(data);
				page.setSuccess(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("保存结算单信息出错：", e);
			page.setSuccess(false);
			page.setMsg("请求出错，请联系管理员！");
			return page;
		}
		return page;
	}

	/**
	 * 
	 * @category 跳向一级结算单列表页面（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月22日 上午9:48:56
	 * @return
	 */
	@RequestMapping("/toSettlementFirstList")
	public String toSettlementFirstList() {
		return "/settle/settlementFirstList";
	}

	/**
	 * 
	 * @category 获取我的结一级算单（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月22日 上午10:28:50
	 * @return
	 */
	@RequestMapping("/getSettlementFirstDataList")
	@ResponseBody
	public Pagination getSettlementFirstDataList(SetSettlementForm form) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String hospId = ((HospInfo) user.getInfo()).getHospId();
		form.setHospId(hospId);
		page.setForm(form);
		return manager.getSettlementFirstDataList(page);
	}

	/**
	 * 
	 * @category 跳向添加一级结算单明细页面（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月22日 上午11:32:35
	 * @param firstlevelSettlementId
	 * @return
	 */
	@RequestMapping("/toAddFirstDetailList")
	public String toAddFirstDetailList() {
		String settlementId = request.getParameter("settlementId");
		request.setAttribute("settlementId", settlementId);
		return "/settle/addFirstDetailList";
	}

	/**
	 * 
	 * @category 获取可加入一级结算单支付明细（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月22日 下午1:52:29
	 * @return
	 */
	@RequestMapping("/getAddFirstDetailDataList")
	@ResponseBody
	public Pagination getAddFirstDetailDataList(String settlementId) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String hospId = ((HospInfo) user.getInfo()).getHospId();
		page.getConditions().put("settlementId", settlementId);
		page.getConditions().put("hospId", hospId);
		page.getConditions().put("drugCode", request.getParameter("drugCode"));// 药品编码
		page.getConditions().put("drugName", request.getParameter("drugName"));// 通用名
		page.getConditions().put("compName", request.getParameter("compName"));// 生产企业
		return manager.getAddFirstDetailDataList(page);
	}

	/**
	 * 
	 * @category 添加一级结算单明细（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月22日 下午3:43:35
	 * @param array
	 * @param settlementId
	 * @return
	 */
	@RequestMapping("/addFirstDetail")
	@ResponseBody
	public Pagination addFirstDetail(String array, String settlementId) {
		Pagination page = new Pagination();
		SetSettlement data = manager.getSetSettlementById(settlementId);
		if (0 != data.getStatus()) {
			page.setSuccess(false);
			page.setMsg("结算单已提交，无法添加明细！");
			return page;
		}
		if (StringUtils.isNotBlank(array)) {
			Gson gson = new Gson();
			Type type = new TypeToken<List<SetSettlementDetail>>() {
			}.getType();
			List<SetSettlementDetail> list = gson.fromJson(array, type);
			SysUser user = (SysUser) request.getSession().getAttribute(
					Constants.USERINFO);
			SetSettlementDetail form = new SetSettlementDetail();
			form.setCreateUser(user.getOrgId());
			form.setSettlementId(settlementId);
			page.setForm(form);
			if (list != null && list.size() > 0) {
				page.setRows(list);
				page = manager.addFirstDetail(page, user);
				return page;
			} else {
				page.setSuccess(false);
				page.setMsg("数据异常，请重试！");
				page.setCode(0);
				return page;
			}
		} else {
			page.setSuccess(false);
			page.setCode(0);
			page.setMsg("请选择要加入的明细！");
			return page;
		}
	}

	/**
	 * 
	 * @category 跳向编辑结算单信息页面（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月22日 下午4:29:41
	 * @param settlementId
	 * @return
	 */
	@RequestMapping("/toEditSettlementFirst")
	@ResponseBody
	public SetSettlement toEditSettlementFirst(String settlementId) {
		SetSettlement data = manager.getSetSettlementById(settlementId);
		return data;
	}

	/**
	 * 
	 * @category 编辑保存一级结算单信息（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月22日 下午4:31:15
	 * @param data
	 * @return
	 */
	@RequestMapping("/editSettlementFirst")
	@ResponseBody
	public Pagination editSettlementFirst(SetSettlement data) {
		Pagination page = new Pagination();
		SetSettlement info = manager.getSetSettlementById(data
				.getSettlementId());
		if (0 != info.getStatus()) {
			page.setSuccess(false);
			page.setMsg("结算单已提交，不可编辑！");
			return page;
		}
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		info.setSettlementName(data.getSettlementName());
		info.setRemarks(data.getRemarks());
		info.setUpdDatetime(new Date());
		info.setUpdUser(user.getId());
		try {
			if (manager.checkSameName(data)) {
				page.setSuccess(false);
				page.setMsg("结算单名称重复，请重新输入！");
			} else {
				manager.editSettlementFirst(info);
				page.setSuccess(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("编辑结算单信息出错：", e);
			page.setSuccess(false);
			page.setMsg("操作失败！");
			return page;
		}
		return page;
	}

	/**
	 * 
	 * @category 删除一级结算单信息（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月22日 下午4:40:23
	 * @param settlementId
	 * @return
	 */
	@RequestMapping("/deleteSettlementFirst")
	@ResponseBody
	public Pagination deleteSettlementFirst(String array) {
		Pagination page = new Pagination();
		if (StringUtils.isNotBlank(array)) {
			Gson gson = new Gson();
			Type type = new TypeToken<List<SetSettlement>>() {
			}.getType();
			List<SetSettlement> list = gson.fromJson(array, type);
			if (list != null && list.size() > 0) {
				page.setRows(list);
				try {
					page = manager.deleteSettlementFirst(page);
					return page;
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("删除一级结算单出错：", e);
					page.setSuccess(false);
					page.setCode(0);
					page.setMsg("请求出错,请联系管理员");
					return page;
				}

			} else {
				page.setSuccess(false);
				page.setCode(0);
				page.setMsg("数据异常，请重试！");
				return page;
			}
		} else {
			page.setSuccess(false);
			page.setCode(0);
			page.setMsg("请选择要删除的结算单！");
			return page;
		}
	}

	/**
	 * 
	 * @category 提交结算单
	 * @author 邱磊
	 * @date 2015年7月22日 下午7:41:10
	 * @param array
	 * @return
	 */
	@RequestMapping("/subSettlementFirst")
	@ResponseBody
	public Pagination subSettlementFirst(String array) {
		Pagination page = new Pagination();
		if (StringUtils.isNotBlank(array)) {
			Gson gson = new Gson();
			Type type = new TypeToken<List<SetSettlement>>() {
			}.getType();
			SysUser user = (SysUser) request.getSession().getAttribute(
					Constants.USERINFO);
			List<SetSettlement> list = gson.fromJson(array, type);
			SetSettlement form = new SetSettlement();
			form.setSubmitUser(user.getOrgId());
			page.setForm(form);
			if (list != null && list.size() > 0) {
				page.setRows(list);
				try {
					page = manager.subSettlementFirst(page);
					return page;
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("提结算单出错：", e);
					page.setSuccess(false);
					page.setCode(0);
					page.setMsg("请求出错,请联系管理员");
					return page;
				}

			} else {
				page.setSuccess(false);
				page.setCode(0);
				page.setMsg("数据异常，请重试！");
				return page;
			}
		} else {
			page.setSuccess(false);
			page.setCode(0);
			page.setMsg("请选择要提交的结算单！");
			return page;
		}
	}

	/**
	 * 
	 * @category 跳向结算详情细页面（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月22日 下午7:42:35
	 * @return
	 */
	@RequestMapping("/toSetSettlementDetailList")
	public String toSetSettlementDetailList(String settlementId) {
		SetSettlementForm data = manager.getSettlementInfoById(settlementId);
		request.setAttribute("info", data);
		return "/settle/setSettlementDetailList";
	}

	/**
	 * 
	 * @category 跳向结算详情细页面（医疗机构）
	 * @author 邱磊
	 * @date 2015年8月6日17:22:34
	 * @return
	 */
	@RequestMapping("/toLookSetSettlementDetailList")
	public String toSeeSetSettlementDetailList(String settlementId) {
		SetSettlementForm data = manager.getSettlementInfoById(settlementId);
		request.setAttribute("info", data);
		return "/settle/lookSetSettlementDetailList";
	}

	/**
	 * 
	 * @category 获取一级结算详情细（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月22日 下午7:53:42
	 * @return
	 */
	@RequestMapping("/getSetSettlementFirstDetailDataList")
	@ResponseBody
	public Pagination getSetSettlemenFirstDetailDataList(String settlementId) {
		Pagination page = new Pagination(request);
		page.getConditions().put("settlementId",
				request.getParameter("settlementId"));
		page.getConditions().put("drugCode", request.getParameter("drugCode"));// 药品编码
		page.getConditions().put("drugName", request.getParameter("drugName"));// 通用名
		page.getConditions().put("compName", request.getParameter("compName"));// 生产企业
		page.getConditions().put("bankAcctTransType",
				request.getParameter("bankAcctTransType"));// 支付类型
		return manager.getSetSettlemenDetailDataList(page);
	}

	/**
	 * 
	 * @category 获取一级结算详情细
	 * @author 邱磊
	 * @date 2015年7月22日 下午7:53:42
	 * @return
	 */
	@RequestMapping("/getSetSettlementFirstDetailList")
	@ResponseBody
	public Pagination getSetSettlemenFirstDetailList(String settlementId) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String heaBurId = "";// 核算中心Id
		if (user.getMain() == Whether.yes.getKey()) {
			heaBurId = user.getOrgId();
		} else { // 非主帐号获取主帐号帐户信息
			heaBurId = user.getRootUser().getOrgId();
		}
		page.getConditions().put("settlementId", settlementId);
		page.getConditions().put("heaBurId", heaBurId);
		page.getConditions().put("hospCode", request.getParameter("hospCode"));// 医疗机构编码
		page.getConditions().put("hospName", request.getParameter("hospName"));// 医疗机构名称
		return manager.getSetSettlemenDetailList(page);
	}

	/**
	 * 
	 * @category 获取一级结算详情细
	 * @author 邱磊
	 * @date 2015年7月22日 下午7:53:42
	 * @return
	 */
	@RequestMapping("/getSetSettlementFirstSeeDetailList")
	@ResponseBody
	public Pagination getSetSettlemenFirstSeeDetailList(String settlementId) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String heaBurId = "";// 核算中心Id
		if (user.getMain() == Whether.yes.getKey()) {
			heaBurId = user.getId();
		} else { // 非主帐号获取主帐号帐户信息
			heaBurId = user.getRootUser().getId();
		}
		page.getConditions().put("settlementId", settlementId);
		page.getConditions().put("heaBurId", heaBurId);
		page.getConditions().put("hospCode", request.getParameter("hospCode"));// 医疗机构编码
		page.getConditions().put("hospName", request.getParameter("hospName"));// 医疗机构名称
		return manager.getSetSettlemenDetailList(page);
	}

	/**
	 * 
	 * @category 删除明细（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月22日 下午8:17:25
	 * @param array
	 * @param firstlevelSettlementId
	 * @return
	 */
	@RequestMapping("/deleteSetSettlementFirstDetail")
	@ResponseBody
	public Pagination deleteSetSettlementFirstDetail(String array,
			String settlementId) {
		Pagination page = new Pagination();
		SetSettlement data = manager.getSetSettlementById(settlementId);
		page.getConditions().put("settlementId", settlementId);
		if (0 != data.getStatus()) {
			page.setSuccess(false);
			page.setMsg("结算单已提交，不可删除明细！");
			return page;
		}
		if (StringUtils.isNotBlank(array)) {
			Gson gson = new Gson();
			Type type = new TypeToken<List<SetSettlementDetail>>() {
			}.getType();
			SysUser user = (SysUser) request.getSession().getAttribute(
					Constants.USERINFO);
			List<SetSettlementDetail> list = gson.fromJson(array, type);
			if (list != null && list.size() > 0) {
				page.setRows(list);
				try {
					manager.deleteSetSettlemenFirstDetail(page, user);
					page.setSuccess(true);
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("删除明细错误：", e);
					page.setSuccess(false);
					page.setMsg("操作失败！");
					return page;
				}
				return page;
			} else {
				page.setSuccess(false);
				page.setMsg("数据异常，请重试！");
				return page;
			}
		} else {
			page.setSuccess(false);
			page.setMsg("请选择要操作的数据！");
			return page;
		}
	}

	/**
	 * 
	 * @category 删除明细（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月22日 下午8:17:25
	 * @param array
	 * @param firstlevelSettlementId
	 * @return
	 */
	@RequestMapping("/delSetSettlementFirstDetail")
	@ResponseBody
	public Pagination delSetSettlementFirstDetail(String array,
			String settlementId) {
		Pagination page = new Pagination();
		SetSettlement data = manager.getSetSettlementById(settlementId);
		page.getConditions().put("settlementId", settlementId);
		if (0 != data.getStatus()) {
			page.setSuccess(false);
			page.setMsg("结算单已提交，无法删除明细！");
			return page;
		}
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		if (StringUtils.isNotBlank(array)) {
			Gson gson = new Gson();
			Type type = new TypeToken<List<SetSettlementDetail>>() {
			}.getType();
			List<SetSettlementDetail> list = gson.fromJson(array, type);
			if (list != null && list.size() > 0) {
				page.setRows(list);
				try {
					manager.delSetSettlemenFirstDetail(page, user);
					page.setSuccess(true);
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("删除明细错误：", e);
					page.setSuccess(false);
					page.setMsg("操作失败！");
					return page;
				}
				return page;
			} else {
				page.setSuccess(false);
				page.setMsg("操作失败！");
				return page;
			}
		} else {
			page.setSuccess(false);
			page.setMsg("请选择要加入的明细！");
			return page;
		}
	}

	/**
	 * 
	 * @category 跳向一级结算单第一次审核页面
	 * @author 邱磊
	 * @date 2015年7月23日 上午9:54:55
	 * @return
	 */
	@RequestMapping("/toAuditFirstOneList")
	public String toAuditFirstOneList() {
		return "/settle/auditSettlementFirstOne";
	}

	/**
	 * 
	 * @category 获取一级结算单第一次审核数据（核算中心）
	 * @author 邱磊
	 * @date 2015年7月23日 上午10:00:54
	 * @return
	 */
	@RequestMapping("/getAuditFirstOneDataList")
	@ResponseBody
	public Pagination getAuditFirstOneDataList(SetSettlementForm form) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String heaBurId = "";// 核算中心Id
		if (user.getMain() == Whether.yes.getKey()) {
			heaBurId = user.getOrgId();
		} else { // 非主帐号获取主帐号帐户信息
			heaBurId = user.getRootUser().getOrgId();
		}
		page.getConditions().put("heaBurId", heaBurId);
		page.getConditions().put("submitBeginDate",
				request.getParameter("submitBeginDate"));
		page.getConditions().put("auditDatetime",
				request.getParameter("auditDatetime"));
		page.setForm(form);
		return manager.getAuditFirstOneDataList(page);
	}

	/**
	 * 
	 * @category 跳向一级结算单第二次审核页面
	 * @author 邱磊
	 * @date 2015年7月23日 上午9:54:55
	 * @return
	 */
	@RequestMapping("/toAuditFirstSecondList")
	public String toAuditFirstSecondList() {
		return "/settle/auditSettlementFirstSecond";
	}

	/**
	 * 
	 * @category 获取一级结算单第二次审核数据（核算中心）
	 * @author 邱磊
	 * @date 2015年7月23日 上午10:00:54
	 * @return
	 */
	@RequestMapping("/getAuditFirstSecondDataList")
	@ResponseBody
	public Pagination getAuditFirstSecondDataList(SetSettlementForm form) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String heaBurId = "";// 核算中心Id
		if (user.getMain() == Whether.yes.getKey()) {
			heaBurId = user.getOrgId();
		} else { // 非主帐号获取主帐号帐户信息
			heaBurId = user.getRootUser().getOrgId();
		}
		page.getConditions().put("heaBurId", heaBurId);
		page.getConditions().put("submitBeginDate",
				request.getParameter("submitBeginDate"));
		page.getConditions().put("auditDatetime",
				request.getParameter("auditDatetime"));
		page.setForm(form);
		return manager.getAuditFirstSecondDataList(page);
	}

	/**
	 * 
	 * @category 跳向二级结算单第一次审核页面（采购中心）
	 * @author 邱磊
	 * @date 2015年7月23日 上午9:54:55
	 * @return
	 */
	@RequestMapping("/toAuditSecondOneList")
	public String toAuditSecondOneList() {
		DicAreaForm area = dicAreaManager.getDicAreaById(sysProvince);
		request.setAttribute("area", area);
		return "/settle/auditSettlementSecondOne";
	}

	/**
	 * 
	 * @category 获取二级结算单第一次审核数据（采购中心）
	 * @author 邱磊
	 * @date 2015年7月23日 上午10:00:54
	 * @return
	 */
	@RequestMapping("/getAuditSecondOneDataList")
	@ResponseBody
	public Pagination getAuditSecondOneDataList() {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String centerId = "";// 中心Id
		if (user.getMain() == Whether.yes.getKey()) {
			centerId = user.getId();
		} else { // 非主帐号获取主帐号帐户信息
			centerId = user.getRootUser().getId();
		}
		page.getConditions().put("centerId", centerId);
		return manager.getAuditSecondOneDataList(page);
	}

	/**
	 * 
	 * @category 跳向二级结算单第二次审核页面（采购中心）
	 * @author 邱磊
	 * @date 2015年7月23日 上午9:54:55
	 * @return
	 */
	@RequestMapping("/toAuditSecondSecondList")
	public String toAuditSecondSecondList() {
		DicAreaForm area = dicAreaManager.getDicAreaById(sysProvince);
		request.setAttribute("area", area);
		return "/settle/auditSettlementSecondSecond";
	}

	/**
	 * 
	 * @category 获取二级结算单第二次审核数据（采购中心）
	 * @author 邱磊
	 * @date 2015年7月23日 上午10:00:54
	 * @return
	 */
	@RequestMapping("/getAuditSecondSecondDataList")
	@ResponseBody
	public Pagination getAuditSecondSecondDataList() {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String centerId = "";// 中心Id
		if (user.getMain() == Whether.yes.getKey()) {
			centerId = user.getId();
		} else { // 非主帐号获取主帐号帐户信息
			centerId = user.getRootUser().getId();
		}
		page.getConditions().put("centerId", centerId);
		return manager.getAuditSecondSecondDataList(page);
	}

	/**
	 * 
	 * @category 跳向一级结算单第一次审核页面（核算中心）
	 * @author 邱磊
	 * @date 2015年7月23日 下午4:54:10
	 * @return
	 */
	@RequestMapping("/toAuditFirstOne")
	public String toAuditFirstOne(String settlementId) {
		SetSettlementForm data = manager.getSettlementInfoById(settlementId);
		request.setAttribute("info", data);
		return "/settle/auditFirstOne";
	}

	/**
	 * 
	 * @category 审核一级结算单第一次（核算中心）
	 * @author 邱磊
	 * @date 2015年7月23日 下午4:45:34
	 * @param form
	 * @return
	 */
	@RequestMapping("/auditFirstOne")
	@ResponseBody
	public Pagination auditFirstOne(SetSettlementForm form) {
		Pagination page = new Pagination();
		SetSettlement data = manager.getSetSettlementById(form
				.getSettlementId());
		if (null != data) {
			if (1 != data.getStatus()) {
				page.setSuccess(false);
				page.setMsg("结算单已被其他用户审核过！");
				return page;
			}
		} else {
			page.setSuccess(false);
			page.setMsg("数据异常，请稍后重试！");
			return page;
		}
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		form.setAuditUser(user.getId());
		try {
			manager.auditFirstOne(form);
			page.setSuccess(true);
			return page;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("一级结算单第一次审核失败：", e);
			page.setSuccess(false);
			page.setMsg("审核失败！");
			return page;
		}
	}

	/**
	 * @category 跳向一级结算单第二次审核页面
	 * @author 邱磊
	 * @date 2015年7月23日 下午4:54:10
	 * @return
	 */
	@RequestMapping("/toAuditFirstSecond")
	public String toAuditFirstSecond(String settlementId) {
		SetSettlementForm form = manager.getSetFirstAuditInfo(settlementId);
		request.setAttribute("info", form);
		return "/settle/auditFirstSecond";
	}

	/**
	 * 
	 * @category 审核一级结算单第二次（核算中心）
	 * @author 邱磊
	 * @date 2015年7月23日 下午4:45:34
	 * @param form
	 * @return
	 */
	@RequestMapping("/auditFirstSecond")
	@ResponseBody
	public Pagination auditFirstSecond(SetSettlement form) {
		Pagination page = new Pagination();
		SetSettlement data = manager.getSetSettlementById(form
				.getSettlementId());
		if (null != data) {
			if (3 < data.getStatus()) {
				page.setSuccess(false);
				page.setMsg("结算单已被其他用户审核过！");
				return page;
			}
		} else {
			page.setSuccess(false);
			page.setMsg("数据异常，请稍后重试！");
			return page;
		}
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		form.setAuditUser(user.getId());
		try {
			manager.auditFirstSecond(form);
			page.setSuccess(true);
			return page;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("一级结算单第一次审核失败：", e);
			page.setSuccess(false);
			page.setMsg("审核失败！");
			return page;
		}
	}

	/**
	 * 
	 * @category 
	 *           跳向一级结算单第一次审核列表页面（采购中心,列表数据调用核算中心取初审列表数据方法"/getAuditFirstOneDataList"
	 *           ）
	 * @author 邱磊
	 * @date 2015年7月23日 上午9:54:55
	 * @return
	 */
	@RequestMapping("/toCeterAuditFirstOneList")
	public String toCeterAuditFirstOneList() {
		return "/settle/cetnerAuditSettlementFirstOne";
	}

	/**
	 * 
	 * @category 跳向一级结算单第一次审核页面（采购中心）
	 * @author 邱磊
	 * @date 2015年7月23日 下午4:54:10
	 * @return
	 */
	@RequestMapping("/toCenterAuditFirstOne")
	public String toCenterAuditFirstOne(String settlementId) {
		DicAreaForm area = dicAreaManager.getDicAreaById(sysProvince);
		request.setAttribute("area", area);
		SetSettlementForm data = manager.getSettlementInfoById(settlementId);
		request.setAttribute("info", data);
		return "/settle/centerAuditFirstOne";
	}

	/**
	 * 
	 * @category 审核一级结算单第一次（采购中心）
	 * @author 邱磊
	 * @date 2015年7月23日 下午4:45:34
	 * @param form
	 * @return
	 */
	@RequestMapping("/cetnerAuditFirstOne")
	@ResponseBody
	public Pagination cetnerAuditFirstOne(SetSettlementForm form) {
		Pagination page = new Pagination();
		SetSettlement data = manager.getSetSettlementById(form
				.getSettlementId());
		if (null != data) {
			if (1 != data.getStatus()) {
				page.setSuccess(false);
				page.setMsg("结算单已被其他用户审核过！");
				return page;
			}
		} else {
			page.setSuccess(false);
			page.setMsg("数据异常，请稍后重试！");
			return page;
		}
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		form.setAuditUser(user.getId());
		try {
			manager.auditFirstOne(form);
			page.setSuccess(true);
			return page;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("一级结算单采购中心第一次审核失败：", e);
			page.setSuccess(false);
			page.setMsg("审核失败！");
			return page;
		}
	}

	/**
	 * 
	 * @category 
	 *           跳向一级结算单第二次审核列表页面（采购中心,列表数据调用核算中心取初审列表数据方法"/getAuditFirstSecondDataList"
	 *           ）
	 * @author 邱磊
	 * @date 2015年7月23日 上午9:54:55
	 * @return
	 */
	@RequestMapping("/toCenterAuditFirstSecondList")
	public String toCenterAuditFirstSecondList() {
		return "/settle/centerAuditSettlementFirstSecond";
	}

	/**
	 * @category 跳向一级结算单第二次审核页面（采购中心）
	 * @author 邱磊
	 * @date 2015年7月23日 下午4:54:10
	 * @return
	 */
	@RequestMapping("/toCenterAuditFirstSecond")
	public String toCenterAuditFirstSecond(String settlementId) {
		DicAreaForm area = dicAreaManager.getDicAreaById(sysProvince);
		request.setAttribute("area", area);
		SetSettlementForm form = manager.getSetFirstAuditInfo(settlementId);
		request.setAttribute("info", form);
		return "/settle/centerAuditFirstSecond";
	}

	/**
	 * 
	 * @category 审核一级结算单第二次（采购中心）
	 * @author 邱磊
	 * @date 2015年7月23日 下午4:45:34
	 * @param form
	 * @return
	 */
	@RequestMapping("/centerAuditFirstSecond")
	@ResponseBody
	public Pagination centerAuditFirstSecond(SetSettlement form) {
		Pagination page = new Pagination();
		SetSettlement data = manager.getSetSettlementById(form
				.getSettlementId());
		if (null != data) {
			if (3 < data.getStatus()) {
				page.setSuccess(false);
				page.setMsg("结算单已被其他用户审核过！");
				return page;
			}
		} else {
			page.setSuccess(false);
			page.setMsg("数据异常，请稍后重试！");
			return page;
		}
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		form.setAuditUser(user.getId());
		try {
			manager.centerAuditFirstSecond(form);
			page.setSuccess(true);
			return page;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("一级结算单第一次审核失败：", e);
			page.setSuccess(false);
			page.setMsg("审核失败！");
			return page;
		}
	}

	/**
	 * 
	 * @category 保存二级结算单信息（核算中心）
	 * @author 邱磊
	 * @date 2015年7月22日 下午3:58:37
	 * @param data
	 * @return
	 */
	@RequestMapping("/saveSettlementSecond")
	@ResponseBody
	public Pagination saveSettlementSecond(SetSettlement data) {
		Pagination page = new Pagination();
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String heaBurId = "";// 核算中心Id
		if (user.getMain() == Whether.yes.getKey()) {
			heaBurId = user.getOrgId();
		} else { // 非主帐号获取主帐号帐户信息
			heaBurId = user.getRootUser().getOrgId();
		}
		data.setSettlementName(data.getSettlementName());
		data.setCreateDatetime(new Date());
		data.setTotalOrders(0);
		data.setPayStatus(0);
		data.setTotalAmount(0.00);

		data.setOrgId(heaBurId);
		data.setCreateUser(user.getId());
		data.setSettlementType(2);
		data.setStatus(0);
		try {
			if (manager.checkSameName(data)) {
				page.setSuccess(false);
				page.setMsg("结算单名称重复，请重新输入！");
			} else {
				manager.saveSettlementSecond(data);
				page.setSuccess(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("保存结算单信息出错：", e);
			page.setSuccess(false);
			page.setMsg("请求出错，请联系管理员！");
			return page;
		}
		return page;
	}

	/**
	 * 
	 * @category 跳向二级结算单列表页面（核算中心）
	 * @author 邱磊
	 * @date 2015年7月22日 上午9:48:56
	 * @return
	 */
	@RequestMapping("/toSettlementSecondList")
	public String toSettlementSecondList() {
		return "/settle/settlementSecondList";
	}

	/**
	 * 
	 * @category 获取二级结算单列表（核算中心）
	 * @author 邱磊
	 * @date 2015年7月22日 上午10:28:50
	 * @return
	 */
	@RequestMapping("/getSettlementSecondDataList")
	@ResponseBody
	public Pagination getSettlementSecondDataList(SetSettlementfirstForm form) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String heaBurId = "";// 核算中心Id
		if (user.getMain() == Whether.yes.getKey()) {
			heaBurId = user.getOrgId();
		} else { // 非主帐号获取主帐号帐户信息
			heaBurId = user.getRootUser().getOrgId();
		}
		form.setHealBurId(heaBurId);
		page.setForm(form);
		return manager.getSettlementSecondDataList(page);
	}

	/**
	 * 
	 * @category 跳向我的结算单查看页面（核算中心）
	 * @author 徐国鹏
	 * @date 2015年7月22日 上午9:48:56
	 * @return
	 */
	@RequestMapping("/toSettlementSecondListSee")
	public String toSettlementSecondListSee() {
		return "/settle/settlementSecondListSee";
	}

	/**
	 * 
	 * @category 获取二级结算单列表（核算中心）
	 * @author 邱磊
	 * @date 2015年7月22日 上午10:28:50
	 * @return
	 */
	@RequestMapping("/getSettlementSecondDataListSee")
	@ResponseBody
	public Pagination getSettlementSecondDataListSee(SetSettlementfirstForm form) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String heaBurId = "";// 核算中心Id
		if (user.getMain() == Whether.yes.getKey()) {
			heaBurId = user.getOrgId();
		} else { // 非主帐号获取主帐号帐户信息
			heaBurId = user.getRootUser().getOrgId();
		}
		form.setHealBurId(heaBurId);
		page.setForm(form);
		return manager.getSettlementSecondDataListSee(page);
	}

	/**
	 * 
	 * @category 跳向结算详情页面（核算中心）
	 * @author 徐国鹏
	 * @date 2015年7月31日 上午11:30:35
	 * @return
	 */
	@RequestMapping("/toSetSettlementDetail")
	public String toSetSettlementDetail(String settlementId) {
		SetSettlementForm data = manager.getSettlementInfoById(settlementId);
		request.setAttribute("info", data);
		return "/settle/setSettlementDetail";
	}

	/**
	 * 
	 * @category 跳向结算查看详情页面（核算中心）
	 * @author 徐国鹏
	 * @date 2015年7月31日 上午11:30:35
	 * @return
	 */
	@RequestMapping("/toSetSettlementSeeDetail")
	public String toSetSettlementSeeDetail(String settlementId) {
		SetSettlementForm data = manager.getSettlementInfoById(settlementId);
		request.setAttribute("info", data);
		return "/settle/setSettlementSeeDetail";
	}

	/**
	 * 
	 * @category 跳向添加二级结算单明细页面（核算中心）
	 * @author 邱磊
	 * @date 2015年7月22日 上午11:32:35
	 * @param firstlevelSettlementId
	 * @return
	 */
	@RequestMapping("/toAddSecondDetailList")
	public String toAddSecondDetailList(String settlementId) {
		SetSettlementForm data = manager.getSettlementInfoById(settlementId);
		request.setAttribute("info", data);
		return "/settle/addSecondDetailList";
	}

	/**
	 * 
	 * @category 获取可加入二级结算单明细（核算中心）
	 * @author 邱磊
	 * @date 2015年7月22日 下午1:52:29
	 * @return
	 */
	@RequestMapping("/getAddSecondDetailDataList")
	@ResponseBody
	public Pagination getAddSecondDetailDataList(String settlementId) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String heaBurId = "";// 核算中心Id
		if (user.getMain() == Whether.yes.getKey()) {
			heaBurId = user.getOrgId();
		} else { // 非主帐号获取主帐号帐户信息
			heaBurId = user.getRootUser().getOrgId();
		}
		page.getConditions().put("settlementId", settlementId);
		page.getConditions().put("heaBurId", heaBurId);
		page.getConditions().put("hospCode", request.getParameter("hospCode"));// 医疗机构编码
		page.getConditions().put("hospName", request.getParameter("hospName"));// 医疗机构名称
		return manager.getAddSecondDetailDataList(page);
	}

	/**
	 * 
	 * @category 添加二级结算单明细（核算中心）
	 * @author 邱磊
	 * @date 2015年7月22日 下午3:43:35
	 * @param array
	 * @param settlementId
	 * @return
	 */
	@RequestMapping("/addSecondDetail")
	@ResponseBody
	public Pagination addSecondDetail(String array, String settlementId) {
		Pagination page = new Pagination();
		SetSettlement data = manager.getSettlementById(settlementId);
		if (0 != data.getStatus()) {
			page.setSuccess(false);
			page.setCode(0);
			page.setMsg("结算单已提交，无法添加明细！");
			return page;
		}
		if (StringUtils.isNotBlank(array)) {
			Gson gson = new Gson();
			Type type = new TypeToken<List<SetSettlementDetail>>() {
			}.getType();
			List<SetSettlementDetail> list = gson.fromJson(array, type);
			SysUser user = (SysUser) request.getSession().getAttribute(
					Constants.USERINFO);
			SetSettlementDetail form = new SetSettlementDetail();
			form.setCreateUser(user.getId());
			form.setSettlementId(settlementId);
			page.setForm(form);
			if (list != null && list.size() > 0) {
				page.setRows(list);
				page = manager.addSecondDetail(page, user);
				return page;
			} else {
				page.setSuccess(false);
				page.setCode(0);
				page.setMsg("数据异常，请重试！");
				return page;
			}
		} else {
			page.setSuccess(false);
			page.setCode(0);
			page.setMsg("请选择要加入的明细！");
			return page;
		}
	}

	/**
	 * 
	 * @category 跳向编辑二级结算单信息页面（核算中心）
	 * @author 邱磊
	 * @date 2015年7月22日 下午4:29:41
	 * @param settlementId
	 * @return
	 */
	@RequestMapping("/toEditSettlementSecond")
	@ResponseBody
	public SetSettlementForm toEditSettlementSecond(String settlementId) {
		SetSettlementForm data = manager.getSettlementInfoById(settlementId);
		// request.setAttribute("info", data);
		return data;
	}

	/**
	 * 
	 * @category 编辑保存二级结算单信息（核算中心）
	 * @author 邱磊
	 * @date 2015年7月22日 下午4:31:15
	 * @param data
	 * @return
	 */
	@RequestMapping("/editSettlementSecond")
	@ResponseBody
	public Pagination editSettlementSecond(SetSettlement data) {
		Pagination page = new Pagination();
		SetSettlement info = manager.getSettlementById(data.getSettlementId());
		if (0 != info.getStatus()) {
			page.setSuccess(false);
			page.setMsg("结算单已提交，不可编辑！");
			return page;
		}
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		info.setSettlementName(data.getSettlementName());
		info.setRemarks(data.getRemarks());
		info.setUpdDatetime(new Date());
		info.setUpdUser(user.getOrgId());
		try {
			if (manager.checkSameName(data)) {
				page.setSuccess(false);
				page.setMsg("结算单名称重复，请重新输入！ ");
			} else {
				manager.editSettlementSecond(info);
				page.setSuccess(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("编辑二级结算单信息出错：", e);
			page.setSuccess(false);
			page.setMsg("请求出错，请联系管理员！");
			return page;
		}
		return page;
	}

	/**
	 * 
	 * @category 删除二级结算单信息（核算中心）
	 * @author 邱磊
	 * @date 2015年7月22日 下午4:40:23
	 * @param settlementId
	 * @return
	 */
	@RequestMapping("/deleteSettlementSecond")
	@ResponseBody
	public Pagination deleteSettlementSecond(String array) {
		Pagination page = new Pagination();
		try {
			if (StringUtils.isNotBlank(array)) {
				Gson gson = new Gson();
				Type type = new TypeToken<List<SetSettlement>>() {}.getType();
				List<SetSettlement> list = gson.fromJson(array, type);
				manager.deleteSettlementSecond(list);
				page.setSuccess(true);
			}
		} catch(MSException e) {
			e.printStackTrace();
			logger.error(e);
			page.setSuccess(false);
			page.setMsg(e.getMsg());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除二级结算单信息出错：", e);
			page.setSuccess(false);
			page.setMsg("操作失败！");
			return page;
		}
		return page;
	}

	/**
	 * 
	 * @category 提交二级结算单（核算中心）
	 * @author 邱磊
	 * @date 2015年7月22日 下午7:41:10
	 * @param array
	 * @return
	 */
	@RequestMapping("/subSettlementSecond")
	@ResponseBody
	public Pagination subSettlementSecond(String array) {
		Pagination page = new Pagination();
		if (StringUtils.isNotBlank(array)) {
			Gson gson = new Gson();
			Type type = new TypeToken<List<SetSettlement>>() {
			}.getType();
			SysUser user = (SysUser) request.getSession().getAttribute(
					Constants.USERINFO);
			List<SetSettlement> list = gson.fromJson(array, type);
			SetSettlement form = new SetSettlement();
			form.setSubmitUser(user.getOrgId());
			page.setForm(form);
			if (list != null && list.size() > 0) {
				page.setRows(list);
				try {
					page = manager.subSettlementSecond(page);
					return page;
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("提交二级结算单出错：", e);
					page.setSuccess(false);
					page.setCode(0);
					page.setMsg("操作失败！");
					return page;
				}

			} else {
				page.setSuccess(false);
				page.setCode(0);
				page.setMsg("操作失败！");
				return page;
			}
		} else {
			page.setSuccess(false);
			page.setCode(0);
			page.setMsg("请选择要加入的明细！");
			return page;
		}
	}

	/**
	 * 
	 * @category 获取一级结算单第一次审核数据
	 * @author 单大伟
	 * @date 2015年7月23日 上午10:00:54
	 * @return
	 */
	@RequestMapping("/getHospSettleList")
	public String getHospSettleList() {
		DicAreaForm area = dicAreaManager.getDicAreaById(sysProvince);
		request.setAttribute("area", area);
		return "/settle/hospSettleList";
	}

	@RequestMapping("/hospSettleAuditList")
	public String getHospSettleAuditList() {
		DicAreaForm area = dicAreaManager.getDicAreaById(sysProvince);
		request.setAttribute("area", area);
		return "/settle/hospSettleAuditList";
	}

	@RequestMapping("/hospSettleReauditList")
	public String getHospSettleReauditList() {
		DicAreaForm area = dicAreaManager.getDicAreaById(sysProvince);
		request.setAttribute("area", area);
		return "/settle/hospSettleReauditList";
	}

	/**
	 * 
	 * @category 复审获取数据
	 * @author 应晓川
	 * @date 2015年7月25日20:11:17
	 * @return
	 */
	@RequestMapping("/toHospSettleReauditList")
	@ResponseBody
	public Pagination toGetHospSettleReauditList() {
		Pagination page = new Pagination(request);
		String area1 = sysProvince;
		String area2 = request.getParameter("area2");
		String area3 = request.getParameter("area3");
		String areaId = AreaUtil.getAreaStartCode(area1, area2, area3);
		page.getConditions().put("areaId", areaId);
		if (StringUtils.isNotBlank(request.getParameter("heaBurName"))) {
			page.getConditions().put("heaBurName",
					request.getParameter("heaBurName"));
		}
		if (StringUtils.isNotBlank(request.getParameter("submitStartDatetime"))) {
			page.getConditions().put("submitStartDatetime",
					request.getParameter("submitStartDatetime"));
		}
		if (StringUtils.isNotBlank(request.getParameter("submitEndDatetime"))) {
			page.getConditions().put("submitEndDatetime",
					request.getParameter("submitEndDatetime"));
		}

		if (StringUtils.isNotBlank(request.getParameter("auditStartDatetime"))) {
			page.getConditions().put("auditStartDatetime",
					request.getParameter("auditStartDatetime"));
		}
		if (StringUtils.isNotBlank(request.getParameter("auditEndDatetime"))) {
			page.getConditions().put("auditEndDatetime",
					request.getParameter("auditEndDatetime"));
		}
		page.getConditions().put("status",
				Constants.SETTLE_STATUS_AUDIT_SUCCESS);
		return manager.hospSettleAuditList(page);
	}

	/**
	 * 
	 * @category 获取一级结算单第一次审核数据
	 * @author 单大伟
	 * @date 2015年7月23日 上午10:00:54
	 * @return
	 */
	@RequestMapping("/hospSettleList")
	@ResponseBody
	public Pagination hospSettleList() {
		Pagination page = new Pagination(request);
		if (StringUtils.isNotBlank(request.getParameter("status"))) {
			page.getConditions().put("status", request.getParameter("status"));
		}

		if (StringUtils.isNotBlank(request.getParameter("heaBurName"))) {
			page.getConditions().put("heaBurName",
					request.getParameter("heaBurName"));
		}

		if (StringUtils.isNotBlank(request.getParameter("submitStartDatetime"))) {
			page.getConditions().put("submitStartDatetime",
					request.getParameter("submitStartDatetime"));
		}

		if (StringUtils.isNotBlank(request.getParameter("submitEndDatetime"))) {
			page.getConditions().put("submitEndDatetime",
					request.getParameter("submitEndDatetime"));
		}

		if (StringUtils.isNotBlank(request.getParameter("auditStartDatetime"))) {
			page.getConditions().put("auditStartDatetime",
					request.getParameter("auditStartDatetime"));
		}

		if (StringUtils.isNotBlank(request.getParameter("auditEndDatetime"))) {
			page.getConditions().put("auditEndDatetime",
					request.getParameter("auditEndDatetime"));
		}

		if (StringUtils
				.isNotBlank(request.getParameter("reauditStartDatetime"))) {
			page.getConditions().put("reauditStartDatetime",
					request.getParameter("reauditStartDatetime"));
		}

		if (StringUtils.isNotBlank(request.getParameter("reauditEndDatetime"))) {
			page.getConditions().put("reauditEndDatetime",
					request.getParameter("reauditEndDatetime"));
		}

		String area1 = sysProvince;
		String area2 = request.getParameter("area2");
		String area3 = request.getParameter("area3");
		String areaId = AreaUtil.getAreaStartCode(area1, area2, area3);
		page.getConditions().put("areaId", areaId);
		return manager.getHospSettleList(page);
	}

	@RequestMapping("/toHospSettleAuditList")
	@ResponseBody
	public Pagination hospSettleAuditList() {
		Pagination page = new Pagination(request);
		String area1 = sysProvince;
		String area2 = request.getParameter("area2");
		String area3 = request.getParameter("area3");
		String areaId = AreaUtil.getAreaStartCode(area1, area2, area3);
		page.getConditions().put("areaId", areaId);
		if (StringUtils.isNotBlank(request.getParameter("heaBurName"))) {
			page.getConditions().put("heaBurName",
					request.getParameter("heaBurName"));
		}
		if (StringUtils.isNotBlank(request.getParameter("submitStartDatetime"))) {
			page.getConditions().put("submitStartDatetime",
					request.getParameter("submitStartDatetime"));
		}
		if (StringUtils.isNotBlank(request.getParameter("submitEndDatetime"))) {
			page.getConditions().put("submitEndDatetime",
					request.getParameter("submitEndDatetime"));
		}
		page.getConditions().put("status", Constants.SETTLE_STATUS_SUBMIT);
		return manager.hospSettleAuditList(page);
	}

	/**
	 * @category 点击要审核的卫生局结算单 “审核”
	 * @author 应晓川
	 * @date 2015年7月24日15:20:05
	 * @return
	 */
	@RequestMapping("/toAuditHospSettleList")
	public String toAuditHospSettleList(String settlementId) {
		SetSettlement setSettlement = manager
				.getSetSettlementById(settlementId);
		SysUser sysUser = null;
		if (StringUtils.isNotBlank(setSettlement.getSubmitUser())) {
			//sysUser = sysUserManager.getUserByOrgId(setSettlement.getSubmitUser());
			sysUser = sysUserManager.getUserById(setSettlement.getSubmitUser());
			//setSettlement.setCreateUser(sysUser.getUsername());
		}
		setSettlement.setTotalAmountStr(ParseNumber.toStrDecimal(setSettlement
				.getTotalAmount()));
		request.setAttribute("obj", setSettlement);
		return "/settle/auditHospSettleList";
	}

	/**
	 * @category 返回 跳转去二级结算单第二次审核数据
	 * @author 应晓川
	 * @dat 2015年7月25日17:20:23
	 * @return
	 */
	@RequestMapping("/toAuditHospSettleListBack")
	public String toAuditHospSettleListBack(String fatherSettlementId) {
		SetSettlement setSettlement = manager
				.getSetSettlementById(fatherSettlementId);
		SysUser sysUser = null;
		if (StringUtils.isNotBlank(setSettlement.getSubmitUser())) {
			sysUser = sysUserManager.getUserById(setSettlement.getSubmitUser());
			setSettlement.setCreateUser(sysUser.getUsername());
		}
		request.setAttribute("obj", setSettlement);
		return "/settle/auditHospSettleList";
	}

	/**
	 * @category 跳转去一级结算单第三次审核数据
	 * @author 应晓川
	 * @date 2015年7月24日19:56:19
	 * @return
	 */
	@RequestMapping("/toAuditChildHospSettleList")
	public String toAuditChildHospSettleList(String settlementId,
			String fatherSettlementId) {
		SetSettlement setSettlement = manager
				.getSetSettlementById(settlementId);
		SysUser sysUser = null;
		if (StringUtils.isNotBlank(setSettlement.getSubmitUser())) {
			sysUser = sysUserManager.getUserById(setSettlement.getSubmitUser());
			setSettlement.setCreateUser(sysUser.getUsername());
		}
		setSettlement.setTotalAmountStr(ParseNumber.toStrDecimal(setSettlement
				.getTotalAmount()));
		request.setAttribute("obj", setSettlement);
		request.setAttribute("fatherSettlementId", fatherSettlementId);
		return "/settle/auditChildHospSettleList";
	}

	/**
	 * @category 跳转去二级结算单第二次审核数据
	 * @author 单大伟
	 * @date 2015年7月24日15:20:05
	 * @return
	 */
	@RequestMapping("/getChildSettlementList")
	@ResponseBody
	public Pagination getChildSettlementList() {
		Pagination page = new Pagination(request);
		String settlementId = request.getParameter("settlementId");

		page.getConditions().put("settlementId", settlementId);

		return manager.getChildSettlementList(page);
	}

	@RequestMapping("/getSetSettletdetailinfoList")
	@ResponseBody
	public Pagination getSetSettletdetailinfoList() {
		Pagination page = new Pagination(request);
		String settlementId = request.getParameter("settlementId");

		page.getConditions().put("settlementId", settlementId);

		return manager.getSetSettletdetailinfoList(page);
	}

	@RequestMapping("/toReAuditHospSettleList")
	public String toReAuditHospSettleList(String settlementId) {
		SetSettlement setSettlement = manager
				.getSetSettlementById(settlementId);
		SysUser sysUser = null;
		if (StringUtils.isNotBlank(setSettlement.getSubmitUser())) {
			sysUser = sysUserManager.getUserById(setSettlement.getSubmitUser());
			//setSettlement.setCreateUser(sysUser.getUsername());
		}
		request.setAttribute("obj", setSettlement);
		return "/settle/reAuditHospSettleList";
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("/toSetSettlementDetailInfo")
	public String toSetSettlementDetailInfo(String fatherSettlementId,
			String settlementId, String transId) {
		Pagination page = new Pagination();
		page.getConditions().put("transId", transId);
		Object obj = manager.getSettlementDetailInfoBysetSettlementId(page);
		if (obj != null) {
			Map<String, Object> map = ((Map<String, Object>) obj);
			BigDecimal proPrice = (BigDecimal) map.get("proPrice");
			if (proPrice != null) {
				map.put("proPrice", ParseNumber.toStrDecimal(proPrice.doubleValue()));
			}
			Integer bankAcctTransType = Integer.valueOf((Byte) map.get("bankAcctTransType"));
			if (bankAcctTransType != null) {
				if (bankAcctTransType == 1) {
					map.put("bankAcctTransType", "采购单");
				} else {
					map.put("bankAcctTransType", "退货单");
				}
			}
		}
		request.setAttribute("obj", obj);
		request.setAttribute("settlementId", settlementId);
		request.setAttribute("fatherSettlementId", fatherSettlementId);
		return "/settle/setSettlementDetailInfo";
	}

	/**
	 * 
	 * @category 跳向初审核查看页面（核算中心，采购中心）
	 * @author 邱磊
	 * @date 2015年7月25日 下午2:19:55
	 * @return
	 */
	@RequestMapping("/toAuditSettlementSeeList")
	public String toAuditSettlementSeeList() {
		DicAreaForm area = dicAreaManager.getDicAreaById(sysProvince);
		request.setAttribute("area", area);
		return "/settle/auditSettlementSee";
	}

	/**
	 * 
	 * @category 跳向结算详情细页面（核算中心）
	 * @author 徐国鹏
	 * @date 2015年7月22日 下午7:42:35
	 * @return
	 */
	@RequestMapping("/toSetSettlementDetailListSee")
	public String toSetSettlementDetailListSee(String settlementId) {
		SetSettlementForm data = manager.getSettlementInfoById(settlementId);
		request.setAttribute("info", data);
		return "/settle/setSettlementDetailListSee";
	}

	/**
	 * 
	 * @category 获取初审核查看页面数据（核算中心，采购中心）
	 * @author 邱磊
	 * @date 2015年7月25日 下午2:21:26
	 * @return
	 */
	@RequestMapping("/getAuditSettlementSeeDataList")
	@ResponseBody
	public Pagination getAuditSettlementSeeDataList(SetSettlementForm form) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String orgId = "";// 核算中心Id
		if (user.getMain() == Whether.yes.getKey()) {
			orgId = user.getOrgId();
		} else { // 非主帐号获取主帐号帐户信息
			orgId = user.getRootUser().getOrgId();
		}
		page.getConditions().put("orgId", orgId);
		page.setForm(form);
		return manager.getAuditSettlementSeeDataList(page);
	}

	/**
	 * 
	 * @category 跳向复审核查看页面（核算中心，采购中心）
	 * @author 邱磊
	 * @date 2015年7月25日 下午2:19:55
	 * @return
	 */
	@RequestMapping("/toRAuditSettlementSeeList")
	public String toRAuditSettlementSeeList() {
		DicAreaForm area = dicAreaManager.getDicAreaById(sysProvince);
		request.setAttribute("area", area);
		return "/settle/rAuditSettlementSee";
	}

	/**
	 * 
	 * @category 获取复审核查看页面数据（核算中心，采购中心）
	 * @author 邱磊
	 * @date 2015年7月25日 下午2:21:26
	 * @return
	 */
	@RequestMapping("/getRAuditSettlementSeeDataList")
	@ResponseBody
	public Pagination getRAuditSettlementSeeDataList(SetSettlementForm form) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String orgId = "";// 核算中心Id
		if (user.getMain() == Whether.yes.getKey()) {
			orgId = user.getId();
		} else { // 非主帐号获取主帐号帐户信息
			orgId = user.getRootUser().getId();
		}
		page.getConditions().put("orgId", orgId);
		page.setForm(form);
		return manager.getRAuditSettlementSeeDataList(page);
	}

	@RequestMapping("/auditPass")
	@ResponseBody
	public Pagination auditPass() {
		Pagination page = new Pagination(request);
		String settlementId = request.getParameter("settlementId");

		if (StringUtils.isBlank(settlementId)) {
			page.setSuccess(false);
			return page;
		}

		String auditRemark = request.getParameter("auditRemark");
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		page.getConditions()
				.put("auditDatetime", DateUtil.dateTime(new Date()));
		page.getConditions().put("settlementId", settlementId);
		page.getConditions().put("auditRemark", auditRemark);
		page.getConditions().put("auditUser", user.getId());
		page.getConditions().put("status",
				Constants.SETTLE_STATUS_AUDIT_SUCCESS);

		page = manager.auditPass(page);
		page.setSuccess(true);
		return page;
	}

	@RequestMapping("/auditUnPass")
	@ResponseBody
	public Pagination auditUnPass() {
		Pagination page = new Pagination(request);
		String settlementId = request.getParameter("settlementId");

		if (StringUtils.isBlank(settlementId)) {
			page.setSuccess(false);
			return page;
		}
		String auditRemark = request.getParameter("auditRemark");
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		page.getConditions()
				.put("auditDatetime", DateUtil.dateTime(new Date()));
		page.getConditions().put("settlementId", settlementId);
		page.getConditions().put("auditRemark", auditRemark);
		page.getConditions().put("auditUser", user.getId());
		page.getConditions()
				.put("status", Constants.SETTLE_STATUS_AUDIT_FAILED);

		page = manager.auditUnPass(page);
		page.setSuccess(true);
		return page;
	}

	/**
	 * @category 复审->详情->详情
	 * @author 应晓川
	 * @date 2015年7月25日20:21:11
	 * @return
	 */
	@RequestMapping("/toReauditChildHospSettleList")
	public String toReauditChildHospSettleList(String settlementId,
			String fatherSettlementId) {
		SetSettlement setSettlement = manager
				.getSetSettlementById(settlementId);
		SysUser sysUser = null;
		if (StringUtils.isNotBlank(setSettlement.getSubmitUser())) {
			sysUser = sysUserManager.getUserById(setSettlement.getSubmitUser());
			setSettlement.setCreateUser(sysUser.getUsername());
		}
		request.setAttribute("obj", setSettlement);
		request.setAttribute("fatherSettlementId", fatherSettlementId);
		return "/settle/reAuditChildHospSettleList";
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("/toReAuditSetSettlementDetailInfo")
	public String toReauditSetSettlementDetailInfo(String fatherSettlementId,
			String settlementId, String transId) {
		Pagination page = new Pagination();
		page.getConditions().put("transId", transId);
		Object obj = manager.getSettlementDetailInfoBysetSettlementId(page);
		if (obj != null) {
			Map<String, Object> map = ((Map<String, Object>) obj);
			BigDecimal proPrice = (BigDecimal) map.get("proPrice");
			if (proPrice != null) {
				map.put("proPrice", ParseNumber.toStrDecimal(proPrice.doubleValue()));
			}
			Integer bankAcctTransType = Integer.valueOf((Byte) map.get("bankAcctTransType"));
			if (bankAcctTransType != null) {
				if (bankAcctTransType == 1) {
					map.put("bankAcctTransType", "采购单");
				} else {
					map.put("bankAcctTransType", "退货单");
				}
			}
		}
		request.setAttribute("obj", obj);
		request.setAttribute("settlementId", settlementId);
		request.setAttribute("fatherSettlementId", fatherSettlementId);
		return "/settle/reAuditSetSettlementDetailInfo";
	}

	/**
	 * @category 通过
	 * @author 应晓川
	 * @date 2015年7月25日20:21:11
	 * @return
	 */
	@RequestMapping("/reAuditPass")
	@ResponseBody
	public Pagination reAuditPass() {
		Pagination page = new Pagination(request);
		String settlementId = request.getParameter("settlementId");

		if (StringUtils.isBlank(settlementId)) {
			page.setSuccess(false);
			return page;
		}

		String reauditRemark = request.getParameter("reauditRemark");
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		page.getConditions().put("reauditDatetime",
				DateUtil.dateTime(new Date()));
		page.getConditions().put("settlementId", settlementId);
		page.getConditions().put("reauditRemark", reauditRemark);
		page.getConditions().put("reauditUser", user.getId());
		page.getConditions().put("status",
				Constants.SETTLE_STATUS_REAUDIT_SUCCESS);

		page = manager.reAuditPass(page);
		page.setSuccess(true);
		return page;
	}

	/**
	 * @category 不通过
	 * @author 应晓川
	 * @date 2015年7月25日20:21:11
	 * @return
	 */
	@RequestMapping("/reAuditUnPass")
	@ResponseBody
	public Pagination reAuditUnPass() {
		Pagination page = new Pagination(request);
		String settlementId = request.getParameter("settlementId");

		if (StringUtils.isBlank(settlementId)) {
			page.setSuccess(false);
			return page;
		}
		String reauditRemark = request.getParameter("reauditRemark");
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		page.getConditions().put("reauditDatetime",
				DateUtil.dateTime(new Date()));
		page.getConditions().put("settlementId", settlementId);
		page.getConditions().put("reauditRemark", reauditRemark);
		page.getConditions().put("reauditUser", user.getId());
		page.getConditions().put("status",
				Constants.SETTLE_STATUS_REAUDIT_FAILED);

		page = manager.reAuditUnPass(page);
		page.setSuccess(true);
		return page;
	}

	@RequestMapping("/toSetSettlementList")
	public String toSetSettlementList() {
		return "/settle/setSettlementList";
	}

	/**
	 * @category 医疗机构查看结算单
	 * @author 应晓川
	 * @date 2015年8月5日09:17:13
	 * @return
	 */
	@RequestMapping("/getSettlementDataList")
	@ResponseBody
	public Pagination getSettlementDataList() {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String hospId = ((HospInfo) user.getInfo()).getHospId();
		DbUtil.jsonToMap(request, page);
		page.getConditions().put("hospId", hospId);
		page = manager.getSettlementDataList(page);
		page.setSuccess(true);
		return page;
	}
	
	/**
	 * @category 中心端->卫生局结算单查看->卫生局结算单详情
	 * @author 应晓川
	 * @date 2015年7月24日15:20:05
	 * @return
	 */
	@RequestMapping("/toSeeHospSettleList")
	public String toSeeHospSettleList(String settlementId) {
		SetSettlement setSettlement = manager
				.getSetSettlementById(settlementId);
		SysUser sysUser = null;
		if (StringUtils.isNotBlank(setSettlement.getSubmitUser())) {
			sysUser = sysUserManager.getUserById(setSettlement.getSubmitUser());
			//setSettlement.setCreateUser(sysUser.getUsername());
		}
		setSettlement.setTotalAmountStr(ParseNumber.toStrDecimal(setSettlement
				.getTotalAmount()));
		request.setAttribute("obj", setSettlement);
		return "/settle/seeHospSettleList";
	}
	
	/**
	 * @category 中心端->卫生局结算单查看->卫生局结算单详情->详情
	 * @author 应晓川
	 * @date 2015年7月24日19:56:19
	 * @return
	 */
	@RequestMapping("/toSeeChildHospSettleList")
	public String toSeeChildHospSettleList(String settlementId,
			String fatherSettlementId) {
		SetSettlement setSettlement = manager
				.getSetSettlementById(settlementId);
		SysUser sysUser = null;
		if (StringUtils.isNotBlank(setSettlement.getSubmitUser())) {
			sysUser = sysUserManager.getUserById(setSettlement.getSubmitUser());
			setSettlement.setCreateUser(sysUser.getUsername());
		}
		setSettlement.setTotalAmountStr(ParseNumber.toStrDecimal(setSettlement
				.getTotalAmount()));
		request.setAttribute("obj", setSettlement);
		request.setAttribute("fatherSettlementId", fatherSettlementId);
		return "/settle/seeChildHospSettleList";
	}
	
	/**
	 * @category 检测名称是否相同
	 * @date 2015年8月11日
	 * @return
	 */
	@RequestMapping("/checkSameName")
	@ResponseBody
	public Pagination checkSameName(SetSettlement settlement) {
		Pagination page = new Pagination(request);
		boolean exists = manager.checkSameName(settlement);
		page.setSuccess(exists);
		return page;
	}
	
}